package com.pkutech.carcasher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.pkutech.carcasher.R;
import com.pkutech.carcasher.bean.BaseResponse;
import com.pkutech.carcasher.bean.LoginResponse;
import com.pkutech.carcasher.db.RealmManager;
import com.pkutech.carcasher.model.UserInfo;
import com.pkutech.carcasher.network.SoapApi;
import com.pkutech.carcasher.utils.CommonUtils;
import com.pkutech.carcasher.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 欢迎页面
 * Created by cyy on 2016/6/5.
 */
public class WelcomeActivity extends AppCompatActivity {

    @BindView(R.id.versionTex)
    TextView versionTex;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        delayJump();

        versionTex.setText("V"+ CommonUtils.getgetVersionName(this));
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("cyy", "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("cyy", "onResume");
    }

    private void autoLogin(UserInfo userInfo){
        SoapApi.login(userInfo.getCode(), userInfo.getPassword())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                    }
                })
                .subscribe(new Subscriber<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        delayJump();
                        ToastUtil.showInfoToast(WelcomeActivity.this, "登陆失败", ToastUtil.Position.TOP);
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        if (loginResponse != null && !TextUtils.isEmpty(loginResponse.getStatus()) && "true".equals(loginResponse.getStatus())) {
                            ToastUtil.showSuccessToast(WelcomeActivity.this, "登陆成功", ToastUtil.Position.TOP);
                            if (loginResponse.getData() != null && loginResponse.getData().size() > 0) {
                                RealmManager.saveUserInfo(loginResponse.getData().get(0));
                            }
                            Intent intent = new Intent(WelcomeActivity.this, HomeActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            delayJump();
                            if (loginResponse != null && !TextUtils.isEmpty(loginResponse.getMessage())) {
                                ToastUtil.showInfoToast(WelcomeActivity.this, loginResponse.getMessage(), ToastUtil.Position.TOP);
                            } else {
                                ToastUtil.showInfoToast(WelcomeActivity.this, "登陆失败", ToastUtil.Position.TOP);
                            }
                        }
                    }
                });
    }

    private void delayJump() {
        Log.d("cyy", "delayJump:" + Thread.currentThread().getName());
        Observable.just(1, 2, 3)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .map(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        try {
                            Thread.sleep(300);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (integer == 3) {
                            return true;
                        }
                        return false;
                    }
                })
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        if (aBoolean) {
                            UserInfo userInfo = RealmManager.findUserInfo();
                            if(userInfo != null){
                                memlogout(userInfo.getCode());
                            }else  {
                                Intent intent = new Intent(WelcomeActivity.this, UserLoginActivity.class);
                                startActivity(intent);
                                WelcomeActivity.this.finish();
                            }
                        }
                    }
                });
    }
    public void memlogout(String account){
        SoapApi.hmLogout(account)
                .subscribe(new Subscriber<BaseResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Intent intent = new Intent(WelcomeActivity.this, UserLoginActivity.class);
                        startActivity(intent);
                        WelcomeActivity.this.finish();
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        Intent intent = new Intent(WelcomeActivity.this, UserLoginActivity.class);
                        startActivity(intent);
                        WelcomeActivity.this.finish();
                    }
                });
    }
}
